package cn.chenhuanming.chuanzhi.service;

import cn.chenhuanming.chuanzhi.dao.StaffDao;
import cn.chenhuanming.chuanzhi.dao.VoteDao;
import cn.chenhuanming.chuanzhi.dao.VoteOptionDao;
import cn.chenhuanming.chuanzhi.util.authority.AuthorityTool;
import cn.chenhuanming.chuanzhi.util.entity.Staff;
import cn.chenhuanming.chuanzhi.util.entity.Vote;
import cn.chenhuanming.chuanzhi.util.entity.VoteOption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.stereotype.Service;

import javax.persistence.LockModeType;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2017-05-08.
 */
@Service
public class VoteService {

    @Autowired
    VoteDao voteDao;

    @Autowired
    VoteOptionDao voteOptionDao;

    @Autowired
    StaffDao staffDao;

    public void newVote(String title, String content, List<String> options) {
        Staff me = staffDao.getOne(AuthorityTool.getPrincipal());
        Vote vote = new Vote();
        vote.setTitle(title);
        vote.setContent(content);
        vote.setStaff(me);
        vote.setCreateDate(LocalDateTime.now());
        voteDao.save(vote);

        options.forEach(name -> {
            VoteOption voteOption = new VoteOption();
            voteOption.setName(name);
            voteOption.setVote(vote);
            voteOptionDao.save(voteOption);
        });
    }

    @Lock(value = LockModeType.PESSIMISTIC_WRITE)
    public void vote(Integer optionId){
        String me = AuthorityTool.getPrincipal();
        VoteOption option = voteOptionDao.getOne(optionId);
        for (String name:option.getApprover().split(",")) {
            if(name.equals(me))
                return;
        }
        if(option.getApprover().equals(""))
            option.setApprover(me);
        else
            option.setApprover(option.getApprover()+","+me);
        voteOptionDao.save(option);
    }

    public void finishVote(String voteId){
        Vote vote = voteDao.getOne(voteId);
        if(vote.getStaff().getUsername().equals(AuthorityTool.getPrincipal())){
            vote.setStatus((short) 0);
            voteDao.save(vote);
        }
    }

    public Page<Vote> listVote(Pageable pageable) {
        return voteDao.findByStatus((short) 1, pageable);
    }

    public Vote findOne(String id) {
        Vote vote = voteDao.findOne(id);
        vote.getVoteOptions();
        return vote;
    }

    public boolean isCanVote(Vote vote){
        if(vote.getStatus()!=1)
            return false;
        String username = AuthorityTool.getPrincipal();
        for (VoteOption voteOption: vote.getVoteOptions()) {
            for (String name:voteOption.getApprover().split(",")) {
                if(name.equals(username))
                    return false;
            }
        }
        return true;
    }
}
